﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hello_accinv
{
    public partial class EditProduct : Form
    {
        private Database db;
        public string loginUser;
        public EditProduct()
        {
            InitializeComponent();
            db = new Database();
        }

        private void btnUpdateProduct_Click(object sender, EventArgs e)
        {
            if (IsValidate())
            {
                db.Update("Product",
                "Name = '" + txtboxName.Text +
                "',UnitPrice = '" + txtboxUnitPrice.Text +
                "',Detail = '" + txtboxMoreDetails.Text + "'",
                "PCode = '" + txtboxPCode.Text + "'");

                db.Insert("ActionLog", "'" + CurrentDateTime.GetDate() + "','" + CurrentDateTime.GetTime() + "','" + loginUser + " edited product " + txtboxName.Text + ".'");

                db.Update("Purchase", "Remark = '" + txtboxRemark.Text + "'", "ProductCode = '" + txtboxPCode.Text + "'");
            }
            else
                MessageBox.Show("Check information", "Error");
        }

        private bool IsValidate()
        {
            bool result = true;
            if (txtboxName.Text == "" && txtboxUnitPrice.Text == "" && txtboxQty.Text == "")
                result = false;
            return result;
        }

        private void btnEditPrices_Click(object sender, EventArgs e)
        {
            txtboxUnitPrice.Enabled ^= true;
            if (txtboxUnitPrice.Enabled)
            {
                btnEditPrices.Text = "Mark";
            }
            else
            {
                btnEditPrices.Text = "Edit";
            }
        }

        private void txtboxUnitPrice_TextChanged(object sender, EventArgs e)
        {
            if (!IsNumeric(txtboxUnitPrice.Text))
            {
                txtboxUnitPrice.Clear();
            }
        }

        private bool IsNumeric(string input)
        {
            string validChars = "0123456789.";
            char inputChar;
            bool result = true;
            for (int i = 0; i < input.Length; i++)
            {
                inputChar = input[i];
                if (validChars.IndexOf(inputChar) == -1)
                {
                    result = false;
                    break;
                }
            }
            return result;
        }

        private string ToSqlDate(string input)
        {
            string[] split = input.Split('/');
            return (Int32.Parse(split[2]) - 543) + "-" + ((split[1].Length == 1) ? "0" : "") + split[1] + "-" + ((split[0].Length == 1) ? "0" : "") + split[0];
        }
    }
}
